﻿using DevExpress.Data;
using DevExpress.XtraEditors;
using DevExpress.XtraGrid;
using DevExpress.XtraGrid.Demos.util;
using DXApplicationTangche.UC.下单MTO;
using DXApplicationTangche.UC.下单MTO.Service;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace DXApplicationTangche.UC.ImpUC
{

    public partial class XtraUserControl团体套号订单一览 : DevExpress.XtraEditors.XtraUserControl
    {
        private List<SaleOrderQueryInfoDTO> SaleOrderQueryInfoDTOs = new List<SaleOrderQueryInfoDTO>();
        public SaleOrderComfirmDTO saleOrderComfirmDTO;
        public List<String> details { get; set; }
        public String detailId { get; set; }
        public Boolean urgent { get; set; }
        public Boolean toBeConfirm { get; set; }

        public XtraUserControl团体套号订单一览()
        {
            InitializeComponent();
            //this.refreshData();
            this.advBandedGridView销售订单一览.BestFitColumns();
            this.advBandedGridView销售订单一览.GroupSummary.AddRange(new GridSummaryItem[] {
                new GridGroupSummaryItem(SummaryItemType.Count, "billNo", null, "(总计= {0})"),
            });
        }
        /// <summary>
        /// 刷新销售订单一览
        /// </summary>
        public void refreshData(List<String> details)
        {
            this.details = details;
            try
            {

                List<SaleOrderQueryDTO> saleOrderQueryDTOs = MTO下单Service.getSaleOrderQueryListByDetailIds(details);
                FunctionHelper.JsonSerialization(saleOrderQueryDTOs);
                this.SaleOrderQueryInfoDTOs.Clear();
                foreach (SaleOrderQueryDTO saleOrderQueryDTO in saleOrderQueryDTOs)
                {
                    //是一个List，用add添加单个对象。传的参就是上一个dto
                    this.SaleOrderQueryInfoDTOs.Add(new SaleOrderQueryInfoDTO(saleOrderQueryDTO));
                }
            }
            catch
            {

            }
            this.gridControl销售订单一览.DataSource = this.SaleOrderQueryInfoDTOs;
            this.advBandedGridView销售订单一览.RefreshData();
        }
        private void advBandedGridView销售订单一览_CustomDrawCell(object sender, DevExpress.XtraGrid.Views.Base.RowCellCustomDrawEventArgs e)
        {
            if (e.Column.Name.Equals("bandedGridColumn销售方式"))
            {
                if ("客户套号".Equals(e.CellValue.ToString()))
                {
                    e.Appearance.BackColor = Color.LightYellow;
                }
            }
            if (e.Column.Name.Equals("bandedGridColumn是否加急"))
            {
                if ("true".Equals(e.CellValue.ToString()))
                {
                    e.Appearance.BackColor = Color.OrangeRed;
                    e.DisplayText = "加急";
                }
                else
                {
                    e.Appearance.BackColor = Color.LightGreen;
                    e.DisplayText = "正常";
                }
            }
            if (e.Column.Name.Equals("bandedGridColumn待确认"))
            {
                if ("true".Equals(e.CellValue.ToString()))
                {
                    e.Appearance.BackColor = Color.OrangeRed;
                    e.DisplayText = "待确认";
                }
                else
                {
                    e.Appearance.BackColor = Color.LightGreen;
                    e.DisplayText = "直接生产";
                }
            }
            if (e.Column.Name.Equals("bandedGridColumn销售渠道"))
            {
                if ("套号团体".Equals(e.CellValue.ToString()))
                {
                    e.Appearance.BackColor = Color.PeachPuff;
                }
            }
        }

        //private void simpleButton批量_Click(object sender, EventArgs e)
        //{
        //    List<string> detailIds = new List<string>();
        //    foreach (SaleOrderQueryInfoDTO item in SaleOrderQueryInfoDTOs)
        //    {
        //        detailIds.Add(item.id);
        //    }
        //    if (detailIds.Count > 0)
        //    {
        //        MTO订单Service.generateOrderCommitDtoAndSaveRelation(detailIds);
        //        MessageBox.Show("完成");
        //    }
        //    else
        //    {
        //        MessageBox.Show("无内容");
        //    }
        //}
        /// <summary>
        /// 销售订单一览右键加急
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void 加急ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.detailId = FunctionHelper.GetAdvBandedgridViewValue(this.advBandedGridView销售订单一览, this.advBandedGridView销售订单一览.FocusedRowHandle, "id");
            //返回值类型是PlainMessageResult，传的参是saleOrderComfirm，调用build方法用 . 
            PlainMessageResult plainMessageResult =
                MTO订单Service.saleOrderComfirm(
                new SaleOrderComfirmDTO(detailId).buildUrgent()
                );
            if (!"200".Equals(plainMessageResult.errorCode))
            {
                MessageBox.Show(plainMessageResult.errorMessage);
            }
        }

        /// <summary>
        /// 销售订单一览右键待确认
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void 待确认ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.detailId = FunctionHelper.GetAdvBandedgridViewValue(this.advBandedGridView销售订单一览, this.advBandedGridView销售订单一览.FocusedRowHandle, "id");
            PlainMessageResult plainMessageResult =
                MTO订单Service.saleOrderComfirm(
                new SaleOrderComfirmDTO(detailId).buildToBeConfirm()
                );
            if (!"200".Equals(plainMessageResult.errorCode))
            {
                MessageBox.Show(plainMessageResult.errorMessage);
            }
        }
        /// <summary>
        /// 右键查看工序
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void 查看生产工序ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.detailId = FunctionHelper.GetAdvBandedgridViewValue(this.advBandedGridView销售订单一览, this.advBandedGridView销售订单一览.FocusedRowHandle, "id");
            new FormMTO工序及发运信息(detailId).ShowDialog();
        }
        /// <summary>
        /// 右键加急
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void 加急ToolStripMenuItem1_Click(object sender, EventArgs e)
        {
            this.detailId = FunctionHelper.GetAdvBandedgridViewValue(this.advBandedGridView销售订单一览, this.advBandedGridView销售订单一览.FocusedRowHandle, "id");
            //返回值类型是PlainMessageResult，传的参是saleOrderComfirm，调用build方法用 . 
            PlainMessageResult plainMessageResult =
                MTO订单Service.saleOrderComfirm(
                new SaleOrderComfirmDTO(detailId).buildUrgent()
                );
            if (!"200".Equals(plainMessageResult.errorCode))
            {
                MessageBox.Show(plainMessageResult.errorMessage);
            }
        }
        /// <summary>
        /// 右键待确认
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void 待确认ToolStripMenuItem1_Click(object sender, EventArgs e)
        {
            this.detailId = FunctionHelper.GetAdvBandedgridViewValue(this.advBandedGridView销售订单一览, this.advBandedGridView销售订单一览.FocusedRowHandle, "id");
            PlainMessageResult plainMessageResult =
                MTO订单Service.saleOrderComfirm(
                new SaleOrderComfirmDTO(detailId).buildToBeConfirm()
                );
            if (!"200".Equals(plainMessageResult.errorCode))
            {
                MessageBox.Show(plainMessageResult.errorMessage);
            }
        }

        private void 取消补货套号销售订单ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            String salesType = FunctionHelper.GetAdvBandedgridViewValue(this.advBandedGridView销售订单一览, this.advBandedGridView销售订单一览.FocusedRowHandle, "salesType");
            if ("套号下单".Equals(salesType) || "客户套号".Equals(salesType))
            {
                String id = FunctionHelper.GetAdvBandedgridViewValue(this.advBandedGridView销售订单一览, this.advBandedGridView销售订单一览.FocusedRowHandle, "id");
                //  销售订单作废
                PlainMessageResult plainMessageResult = MTO订单Service.saleOrderDeprecate(id);
                if (!"200".Equals(plainMessageResult.errorCode))
                {
                    MessageBox.Show(plainMessageResult.errorMessage);
                }
                else
                {
                    MessageBox.Show("取消成功");
                }
            }
            else{
                MessageBox.Show("仅用于取消套号相关销售订单");
            }
        }
    }
}
